package handlers

import (
	"encoding/json"
	"game-sys/cache"
	"game-sys/models"
	"github.com/gin-gonic/gin"
	"time"
)

func DevilList(c *gin.Context) {
	var devil []models.Devil
	redis_data_key := "devil_list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_data_key).Result()
	if cacheData == "" {
		models.DB.Find(&devil)
		jsonData, _ := json.Marshal(devil)
		cache.Rdb.Set(cache.Ctx, redis_data_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &devil)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": devil,
		"msg":  "查询成功",
	})
	return
}
